﻿using System;
using System.Text;
using System.Collections;

namespace mfXmlSerializer
{
    /// <summary>
    /// represents a specialized collection type for <cref="mfXmlSerializer.mfMapBaseElement"/>
    /// </summary>
    public class mfMapElementCollection : ArrayList
    {
        #region CTOR

        public mfMapElementCollection() :
            base()
        {

        }
        #endregion

        #region ArrayList-Members
        private mfMapBaseElement AssertIfTypeIsInvalid(mfMapBaseElement value)
        {
            var typedValue = value as mfMapBaseElement;

            if (typedValue == null)
                throw new Exception(GetType() + ": value '" + value.ToString() + "' is no valid object from type '" + GetType() + "'");

            return typedValue;
        }

        public new mfMapBaseElement this[int index]
        {
            get
            {
                return base[index] as mfMapBaseElement;
            }
            set
            {
                var typedValue = AssertIfTypeIsInvalid(value);

                base[index] = typedValue;
            }
        }

        public int Add(mfMapBaseElement value)
        {
            var typedValue = AssertIfTypeIsInvalid(value);
            return base.Add(typedValue);
        }

        public int BinarySearch(mfMapBaseElement value, IComparer comparer)
        {
            var typedValue = AssertIfTypeIsInvalid(value);

            return base.BinarySearch(typedValue, comparer);
        }

        public bool Contains(mfMapBaseElement value)
        {
            var typedValue = AssertIfTypeIsInvalid(value);

            return base.Contains(typedValue);
        }

        public int IndexOf(mfMapBaseElement value)
        {
            var typedValue = AssertIfTypeIsInvalid(value);

            return base.IndexOf(typedValue);
        }

        public int IndexOf(mfMapBaseElement value, int startIndex)
        {
            var typedValue = AssertIfTypeIsInvalid(value);

            return base.IndexOf(typedValue, startIndex);
        }

        public int IndexOf(mfMapBaseElement value, int startIndex, int count)
        {
            var typedValue = AssertIfTypeIsInvalid(value);

            return base.IndexOf(typedValue, startIndex, count);
        }

        public void Insert(int index, mfMapBaseElement value)
        {
            var typedValue = AssertIfTypeIsInvalid(value);

            base.Insert(index, typedValue);
        }

        public void Remove(mfMapBaseElement obj)
        {
            var typedValue = AssertIfTypeIsInvalid(obj);

            base.Remove(typedValue);
        }

        #endregion
    }
}
